package annular;

/**
 * @ClassName 普通的队列
 * @DeScription TraditionQueue
 * @Author 剧情再美终是戏
 * @Date 2019/11/18 21:19
 * @Version 1.0
 **/
public class AnnularQueue {

    /**
     * 最大的长度
     **/
    private int maxSize;

    /**
     * 当前队列的头部
     **/
    private int front;

    /**
     * 当前队列的尾部
     **/
    private int rear;

    /**
     * 集合
     **/
    private int[] arrs;

    public AnnularQueue(int maxSize) {
        this.maxSize = maxSize;
        arrs = new int[maxSize];
    }

    public AnnularQueue() {
    }

    // 判断队列是否为空
    public boolean isEmpty() {
        return rear == front;
    }

    // 判断队列是否满了
    public boolean isFull() {
        return (rear + 1) % maxSize == front;
    }

    // 向队列添加数据
    public void add(int n) {
        if (isFull()) {
            System.out.println("队列满了~");
            return;
        }

        arrs[rear] = n;
        rear = (rear + 1) % maxSize;
    }

    // 在队列获取一个数据
    public int get() {
        if (isEmpty()) {
            throw new RuntimeException("队列是空的~");
        }

        int result = arrs[front];
        front = (front + 1) % maxSize;
        return result;
    }

    // 显示队列
    public void show() {
        if (isEmpty()) {
            System.out.println("队列是空的~");
            return;
        }
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arrs[%d]=%d\n", i % maxSize, arrs[i % maxSize]);
        }
    }

    public int size() {
        // rear =1
        // front =0
        // maxsize=4
        return (rear + maxSize - front) % maxSize;
    }

    // 查看队列当前值
    public int head() {
        if (isEmpty()) {
            throw new RuntimeException("队列是空的~");
        }
        return arrs[front % maxSize];
    }

    public int getMaxSize() {
        return maxSize;
    }

    public void setMaxSize(int maxSize) {
        this.maxSize = maxSize;
    }

    public int getFront() {
        return front;
    }

    public void setFront(int front) {
        this.front = front;
    }

    public int getRear() {
        return rear;
    }

    public void setRear(int rear) {
        this.rear = rear;
    }

    public int[] getArrs() {
        return arrs;
    }

    public void setArrs(int[] arrs) {
        this.arrs = arrs;
    }
}
